#include "player.h"
#include "playertable.h"
#include "gamedialog.h"
#include <QDebug>

player::player(
    QGraphicsObject* parent,
    QString path, QString name, QString id, PlayerColor color,bool skillUsed,
    int resist0, int resist1, int resist2, int resist3, int resist4,
    int buddhaNum, int YinyangNum, int qiNum, int positionx,int positiony,const QPixmap &iconPix):
    QGraphicsObject(parent),
    _imgpath(path), _playername(name), _playerid(id),_playercolor(color),
    _skilled(skillUsed),
    _resist0(resist0), _resist1(resist1),
    _resist2(resist2), _resist3(resist3),_resist4(resist4),
    _buddhaNum(buddhaNum),_yingyangNum(YinyangNum),_qiNum(qiNum),
    _px(positionx),_py(positiony),_iconPix(iconPix)

{
    _tilePos = setTilePos(color);
    int length = TileBoard::TILE_WIDTH;
    this->setPos(_iconPix.width()/2*(2*_tilePos.x()-1)-length*(1-_px), _iconPix.height()*(2*_tilePos.y()-1)/2-length*(1-_py));

    connect(this,SIGNAL(pressed()),this,SLOT(makeAmove()));
    this->setZValue(zValue()+1);
    setAcceptHoverEvents(true);
}
void player::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    qreal myscale = 1;
    bool over = option->state & QStyle::State_MouseOver;
    if( over )
    {
        myscale = 1.1;
    }
    painter->drawPixmap(-_iconPix.width()/2*myscale, -_iconPix.height()/2*myscale,
                        _iconPix.width()*myscale,_iconPix.height()*myscale, _iconPix);
}

QRectF player::boundingRect() const{
    return QRectF(-_iconPix.width()/2, -_iconPix.height()/2, _iconPix.width(), _iconPix.height());
}

QString player::ColorToString( PlayerColor c )
{
    switch( c )
    {
        case RED:
            return tr("red");
        case YELLOW:
            return tr("yellow");
        case GREEN:
            return tr("green");
        case BLUE:
            return tr("blue");
    }
    return tr("Not defined");
}

QString player::ColorToSkillString( PlayerColor c )
{
    switch( c )
    {
        case RED:
            return tr("red skill");
        case YELLOW:
            return tr("yellow skill");
        case GREEN:
            return tr("green");
        case BLUE:
            return tr("blue");
    }
    return tr("Not defined");
}

QPoint player::setTilePos(PlayerColor c){
    switch( c )
    {
        case RED:
            return QPoint(0,0);
        case YELLOW:
            return QPoint(0,1);
        case GREEN:
            return QPoint(1,0);
        case BLUE:
            return QPoint(1,1);
    default:
        return QPoint(-1,-1); //error
    }

}

void player::resistChange0(bool isIncease)
{
    int current0 = player::getResist0();
    if(isIncease)
        player::setResist0(current0+1);
    else
        if(current0>0) //make sure the number is above 0
            player::setResist0(current0-1);
}

void player::resistChange1(bool isIncease)
{
    int current1 = player::getResist1();
    if(isIncease)
        player::setResist1(current1+1);
    else
        if(current1>0) //make sure the number is above 0
            player::setResist1(current1-1);
}

void player::resistChange2(bool isIncease)
{
    int current2 = player::getResist2();
    if(isIncease)
        player::setResist2(current2+1);
    else
        if(current2>0) //make sure the number is above 0
            player::setResist1(current2-1);
}

void player::resistChange3(bool isIncease)
{
    int current3 = player::getResist3();
    if(isIncease)
        player::setResist3(current3+1);
    else
        if(current3>0) //make sure the number is above 0
            player::setResist3(current3-1);
}

void player::resistChange4(bool isIncease)
{
    int current4 = player::getResist4();
    if(isIncease)
        player::setResist4(current4+1);
    else
        if(current4>0) //make sure the number is above 0
            player::setResist4(current4-1);
}

void player::yingyangChange(bool isIncease)
{
    int current_yingyang = player::getYingyang();
    if(isIncease)
        player::setYingyang(current_yingyang+1);
    else
        if(current_yingyang>0) //make sure the number is above 0
            player::setYingyang(current_yingyang-1);
}

void player::qiChange(bool isIncease)
{
    int current_qi = player::getQi();
    if(isIncease)
        player::setQi(current_qi+1);
    else
        if(current_qi>0) //make sure the number is above 0
            player::setQi(current_qi-1);

}
void player::checkDead()
{
    int current_qi = player::getQi();
    if(current_qi>0)
        player::setDead(false);
    //player dead then all the fabao and yingyang will set to 0
    else{
        player::setDead(true);
        player::setResist0(0);
        player::setResist1(0);
        player::setResist2(0);
        player::setResist3(0);
        player::setResist4(0);
        player::setYingyang(0);
    }
}
void player::mousePressEvent(QGraphicsSceneMouseEvent *event){
    emit pressed();
    update();
}

void player::makeAmove(){

    PlayerTable* table = dynamic_cast<PlayerTable*>(this->parentObject()->parentObject());
    ActionGuideDialog g(table, "Choose a village to move");
    VillageTile* clicked = table->askForVillageTile();
    moveAnim(clicked);


    update();
}

void player::moveAnim(VillageTile* vt){
    QPoint targetP = vt->getVillagePos();
    int length = TileBoard::TILE_WIDTH;

    int currentX = _iconPix.width()/2*(2*_tilePos.x()-1)-length*(1-_px);
    int currentY = _iconPix.height()*(2*_tilePos.y()-1)/2-length*(1-_py);

    this->setPx( targetP.x());
    this->setPy( targetP.y());

    int movedX = _iconPix.width()/2*(2*_tilePos.x()-1)-length*(1-_px);
    int movedY = _iconPix.height()*(2*_tilePos.y()-1)/2-length*(1-_py);

    GameParAnimation* movement = new GameParAnimation(this);
    movement->addMove(this,QPointF(currentX,currentY),QPointF(movedX,movedY),300);
    movement->start(QAbstractAnimation::DeleteWhenStopped);
    connect(movement, SIGNAL(finished()),this,SLOT(makeAact()));
    this->setPos(_iconPix.width()/2*(2*_tilePos.x()-1)-length*(1-_px), _iconPix.height()*(2*_tilePos.y()-1)/2-length*(1-_py));

}

void player::makeAact(){
    PlayerTable* table = dynamic_cast<PlayerTable*>(this->parentObject()->parentObject());
    TileBoard* tile = table->getTileBorad();
    VillageTile* vt = tile->getVillageTile(_px,_py);
    qDebug() << "current vt:" << vt->name();
    qDebug() << "current pos:" << QPoint(_px, _py);
    vt->startAshop(vt);


}

//------------------------------------//
// PlayerInfo
//------------------------------------//

/*
PlayerInfo::PlayerInfo(player::PlayerColor c)
{

    _mainlayout = new QVBoxLayout( this );
    _infolayout = new QGridLayout;

    _namelabel  = new QLabel;
    _idlabel    = new QLabel;
    _imglabel   = new QLabel;

    //_colorlabel = new QLabel;

    _rredlabel = new QLabel;
    _ryellabel = new QLabel;
    _rblulabel = new QLabel;
    _rgrelabel = new QLabel;
    _rblalabel = new QLabel;

    _qilabel    = new QLabel;
    _yingyanglabel = new QLabel;
    _buddlabel    = new QLabel;

    _mainlayout->addWidget( _namelabel );
    _mainlayout->addWidget( _imglabel );
    _mainlayout->addStretch();
    _mainlayout->addLayout( _infolayout );

    //_namelabel->setAlignment(AlignHCenter);

    _infolayout->addWidget( new QLabel( tr("ID") ),          0, 0 );
    _infolayout->addWidget( _idlabel,                        0, 1 );
    //_infolayout->addWidget( new QLabel( tr("Color") ),       1, 0 );
    //_infolayout->addWidget( _colorlabel,                     1, 1 );
    _infolayout->addWidget( new QLabel( tr("Red power") ),   1, 0 );
    _infolayout->addWidget( _rredlabel,                      1, 1 );
    _infolayout->addWidget( new QLabel( tr("Yellow power") ),2, 0 );
    _infolayout->addWidget( _ryellabel,                      2, 1 );
    _infolayout->addWidget( new QLabel( tr("Blue power") ),  3, 0 );
    _infolayout->addWidget( _rblulabel,                      3, 1 );
    _infolayout->addWidget( new QLabel( tr("Green power") ), 4, 0 );
    _infolayout->addWidget( _rgrelabel,                      4, 1 );
    _infolayout->addWidget( new QLabel( tr("Black power") ), 5, 0 );
    _infolayout->addWidget( _rblalabel,                      5, 1 );
    _infolayout->addWidget( new QLabel( tr("Qi") ),          6, 0 );
    _infolayout->addWidget( _qilabel,                        6, 1 );
    _infolayout->addWidget( new QLabel( tr("Yingyang") ),    7, 0 );
    _infolayout->addWidget( _yingyanglabel,                  7, 1 );
    _infolayout->addWidget( new QLabel( tr("Budda") ),       8, 0 );
    _infolayout->addWidget( _buddlabel,                      8, 1 );

    displayPlayerInfo(c);
    QPushButton* skillButton = new QPushButton( tr("Use skill") );
    connect( skillButton, SIGNAL( pressed() ), this, SLOT( buttonClicked() ) );
    _mainlayout->addWidget( skillButton );


}


void PlayerInfo::displayPlayerInfo(player::PlayerColor c)
{
    switch ( c )
    {
    case player::BLUE:
        _testPlayer = new player( 0, tr(":/common/images/caocao.png"),
                                tr("Blue player"), 0, player::BLUE,false,
                                0, 0, 0, 0, 0, 0, 1, 3, 0, 0 );
        break;
    case player::RED:
        _testPlayer = new player( 0, tr(":/common/images/huangzhong.png"),
                                tr("Red player"), 0, player::RED,false,
                                0, 0, 0, 0, 0, 0, 1, 3, 0, 0 );
        break;
    case player::YELLOW:
        _testPlayer = new player( 0, tr(":/common/images/gaoshun.png"),
                                tr("Yellow player"), 0, player::YELLOW,false,
                                0, 0, 0, 0, 0, 0, 1, 3, 0, 0 );
        break;
    case player::GREEN:
        _testPlayer = new player( 0, tr(":/common/images/ganning.png"),
                                tr("Green player"), 0, player::GREEN,false,
                                0, 0, 0, 0, 0, 0, 1, 3, 0, 0 );
        break;

    }
    _namelabel->setText( _testPlayer->getName() );
    _idlabel->setText(  _testPlayer->getID()  );
    _imglabel->setPixmap( QPixmap( _testPlayer->getImagePath() ) );


    //_colorlabel->setText( player::ColorToString( _testPlayer->getColor() )  );

    //_pskilllabel->setText( player::ColorToSkillString( _testPlayer->getColor() ) );

    _rredlabel->setNum(  _testPlayer->getResist0()  );
    _ryellabel->setNum(  _testPlayer->getResist1()  );
    _rblulabel->setNum(  _testPlayer->getResist2()  );
    _rgrelabel->setNum(  _testPlayer->getResist3()  );
    _rblalabel->setNum(  _testPlayer->getResist4()  );

    _qilabel->setNum(  _testPlayer->getQi()  );
    _yingyanglabel->setNum(  _testPlayer->getYingyang()  );
    _buddlabel->setNum(  _testPlayer->getBudd()  );

}

void PlayerInfo::buttonClicked()
{
    //use player skills

}

*/

